package tech.xiaozai.rbac.mapstruct;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import tech.xiaozai.rbac.bo.UserBO;
import tech.xiaozai.rbac.custom.po.UserPO;

import java.util.LinkedList;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-09-21 09:47
 */

@Mapper
public interface UserMapStruct {

    UserMapStruct me = Mappers.getMapper(UserMapStruct.class);

    /**
     *  ...
     * @param user
     * @return
     */
    @Mapping(target = "roleIds",ignore = true)
    UserBO toBo(UserPO user);


    /**
     *   ...
     * @param userList
     * @return
     */
    default List<UserBO> toBoList(List<UserPO> userList){
        List<UserBO> list = new LinkedList<>();
        userList.forEach(po->{
            UserBO bo = toBo(po);
            if(po.getRoles()!=null){
                bo.setRoleIds(po.getRoles().stream().mapToLong(r->r.getId()).collect(LinkedList::new,List::add,List::addAll));
            }
            list.add(bo);
        });
        return  list;
    }

}
